from DAL.sqlHelper import get_keyboard_conn as conn_a,SQL_ADD_FIELD_VALUES,SQL_UPDATE_SET

class paymentInfoDAL:
    def getList(self,id=0):
        sql ="""
         SELECT * FROM `charge1_payment_info`
        """
        # print(sql)
        try:
            conn = conn_a()
            with conn.cursor() as cursor:
                cursor.execute(sql)
                conn.commit()
                res = cursor.fetchall()
                return res
        finally:
            conn.close()

    def getListById(self,id=0):
        sql = """
              SELECT * FROM `charge1_payment_info`
              WHERE id='{0}'
              """
        sql = sql.format(id)
        # print(sql)
        try:
            conn = conn_a()
            with conn.cursor() as cursor:
                cursor.execute(sql)
                conn.commit()
                res = cursor.fetchone()
                return res
        finally:
            conn.close()

    def getListByParm(self):
        sql = """
            SELECT * FROM `charge1_payment_info`
              """
        try:
            conn = conn_a()
            with conn.cursor() as cursor:
                cursor.execute(sql)
                conn.commit()
                res = cursor.fetchall()
                return res
        finally:
            conn.close()

    def add(self,salesman=None, account=None, money=None
               , order_form=None, order_status=None,
               payment_time=None, recharge_status=None
               ,game=None, return_message=None):
        sql_field,sql_values=SQL_ADD_FIELD_VALUES(locals())
        sql = """
               INSERT INTO   `charge1_payment_info`({0},create_time)
               VALUES({1},NOW())
               """
        sql = sql.format(sql_field,sql_values)
        # print(sql)
        try:
            conn = conn_a()
            with conn.cursor() as cursor:
                cursor.execute(sql)
                conn.commit()
                res = 1
                return res
        finally:
            conn.close()

    def update(self,id=0,salesman=None, account=None, money=None
               , order_form=None, order_status=None,
               payment_time=None, recharge_status=None
               ,game=None, return_message=None):
        sql_set = SQL_UPDATE_SET(locals(),['id'])
        sql = """
              UPDATE 
                `charge1_payment_info`
                SET  {1}
                WHERE id = '{0}'
              """
        sql = sql.format(id,sql_set)
        # print(sql)
        try:
            conn = conn_a()
            with conn.cursor() as cursor:
                cursor.execute(sql)
                conn.commit()
                res = 1
                return res
        finally:
            conn.close()

    def removeById(self,id=0):
        sql = """
                DELETE 
                FROM  `charge1_payment_info`
                WHERE  id='{0}'
              """
        sql = sql.format(id)
        # print(sql)
        try:
            conn = conn_a()
            with conn.cursor() as cursor:
                cursor.execute(sql)
                res = 1
                return res
        finally:
            conn.close()




if __name__ == "__main__":
     # 调试部分---------------------------
     dal = paymentInfoDAL()
     res = dal.getListById(1)
     print(res)
     # res = dal.add(salesman="admin")
     # dal.update(id=1,account=11)
